package com.ckd.flyingtrip.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.javabean.UserInfoBean;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 个人中心   飞哥出行
 */
public class MyselfCenterActivity extends BaseActivity {

    @BindView(R.id.cd_title_img)
    ImageView cdTitleImg;
    @BindView(R.id.cd_close)
    TextView cdClose;
    @BindView(R.id.btn_sz)
    TextView btnSz;
    @BindView(R.id.name_title_shezhi_img)
    ImageView nameTitleShezhiImg;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.txt_shenfen)
    TextView txtShenfen;
    @BindView(R.id.btn_zhuce)
    LinearLayout btnZhuce;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.txt_youhuijuan)
    TextView txtYouhuijuan;
    @BindView(R.id.txt_yajin)
    TextView txtYajin;
    @BindView(R.id.btn_qianbao)
    LinearLayout btnQianbao;
    @BindView(R.id.btn_cheliang)
    LinearLayout btnCheliang;
    @BindView(R.id.btn_fuwuzhan)
    LinearLayout btnFuwuzhan;
    @BindView(R.id.btn_guzhang)
    LinearLayout btnGuzhang;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myself_center);
        ButterKnife.bind(this);
        txtPhone.setText(BcUtils.getUserInfo().getUserPhone());
        volleyUserInfo();

    }

    @OnClick({R.id.cd_close, R.id.btn_sz, R.id.btn_xinxi, R.id.btn_zhuce, R.id.btn_qianbao, R.id.btn_cheliang, R.id.btn_fuwuzhan, R.id.btn_guzhang,R.id.btn_che})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cd_close:
                setResult(RESULT_OK);
                finish();
                break;
            case R.id.btn_sz:
                startAction(OptionActivity.class);
                break;
            case R.id.btn_xinxi:
                startAction(UserInfoActivity.class);
                break;
            case R.id.btn_zhuce:
//                我的车辆
                startAction(MyVehicleActivity.class);
                break;
            case R.id.btn_qianbao:
                startAction(MyWallet.class);
                break;
            case R.id.btn_cheliang:
                break;
            case R.id.btn_fuwuzhan:
                break;
            case R.id.btn_guzhang:
                startAction(MyfaultActivity.class);
                break;
            case R.id.btn_che:
//                我的订单
                startAction(MyDingdanActity.class);
                break;
        }
    }
    public void volleyUserInfo() {
        final Gson mGson = new Gson();
        String url = Constants.QINGQIUGERENXINXI;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("用户信息", s);
                //s为请求返回的字符串数据
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        JSONObject dataInfo = json.getJSONObject("data");
                        String data = json.getString("data");
                        UserInfoBean userInfoBean = mGson.fromJson(data, UserInfoBean.class);
                        txtYajin.setText(userInfoBean.getUserDepositNum());

                    } else {
                        Log.i("站好","账号");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dismisProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {//创建请求参数

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId",BcUtils.getUid());
                    json_url.put("userPhone", BcUtils.getUserInfo().getUserPhone());
                    json_url.put("token", BcUtils.getToken());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }


}
